<?php 
	require_once($_SERVER['DOCUMENT_ROOT']."/model/data_access.php");
    require_once($_SERVER['DOCUMENT_ROOT']."/model/helper.php");
    require_once($_SERVER['DOCUMENT_ROOT']."/configures/global.php");
    $source = array_key_exists("source",$_GET)?$_GET["source"]:"";
    $lan = get_lan();
    $divided_src = explode(",",$source,3);
    $categories_count_tmp = [0,0,0,0,0];
    if (count($divided_src) == 2)
	{
        $categories_count_tmp = count_categories_entity( NULL,$divided_src[1],$divided_src[0],function($buf,$item){return $buf+=$item["CNTEntity"];});
   }
   else if (count($divided_src) == 3)
   {
        $categories_count_tmp = count_categories_entity($divided_src[2], $divided_src[1],$divided_src[0],function($buf,$item){return $buf+=$item["CNTEntity"];});
    }    
   $arr = [];
    $categories_count[Categories::getCategorieName(Categories::ACCOMMODATION,$lan)] = $categories_count_tmp["accommodation"] ;
    $categories_count[Categories::getCategorieName(Categories::TRANSPORT,$lan)] = $categories_count_tmp["attraction"] ;
    $categories_count[Categories::getCategorieName(Categories::SHOPPING,$lan)] = $categories_count_tmp["food_beverage"] ;
    $categories_count[Categories::getCategorieName(Categories::ATTRACTION,$lan)] = $categories_count_tmp["shopping"] ;
    $categories_count[Categories::getCategorieName(Categories::FOOD,$lan)] = $categories_count_tmp["transport"] ;
    unset($categories_count_tmp);
    //Associative array has ordering so it will work..... 
    arsort($categories_count);
    $categories = [];
    $end_of_source = end($divided_src);
    $total = 0;
    foreach ($categories_count as $key=>$value)
    {
        if ($value > 0)
        {
            $id = reverse_category_name2Id($key);
            array_push($categories,["text"=>$key,"imageSrc"=>IMG."landing_home/menu_icon_0".$id.".gif","value"=>$id,"selected"=>False,"description"=>$lan==DEFAULT_LN?"{$end_of_source} 在{$key} 中搜索 ({$value} 结果)":"{$end_of_source} search in {$key} ({$value} results)"]);
            $total+=$value;
        }
    }
    array_push($categories,["text"=>$lan==DEFAULT_LN?"全部分类":"All categories","value"=>-1,"imageSrc"=>IMG."landing_home/menu_icon_06.gif","selected"=>True,"description"=>$lan==DEFAULT_LN?"{$end_of_source} 在全部分类中搜索 ({$total} 结果)":"{$end_of_source} search in all categories({$total} result)"]);
	echo json_encode($categories);
?>